//
//  AuthenCompanyMercVM.swift
//  kxf
//
//  Created by Mac on 2024/3/8.
//

import Foundation
import UIKit
import MCToast

class AuthenCompanyMercVM: UIView {
    
    var selfHeight = kFitWidth(10)
    var channel = "ys"
    
    var nextBlock:(()->())?
    
    override init(frame:CGRect){
        super.init(frame: CGRect.init(x: 0, y: frame.origin.y, width: SCREEN_WIDHT, height: frame.size.height))
        self.selfHeight = frame.size.height
        self.backgroundColor = .COLOR_LIGHT_GREY
        self.isUserInteractionEnabled = true
        
        initUI()
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    lazy var scrollView : UIScrollView = {
        let scro = UIScrollView.init(frame: CGRect.init(x: 0, y: 0, width: SCREEN_WIDHT, height: self.selfHeight))
        scro.backgroundColor = .COLOR_LIGHT_GREY
        if #available(iOS 11.0, *) {
            scro.contentInsetAdjustmentBehavior = .never
        } else {
            // Fallback on earlier versions
        }
        scro.bounces = true
        
        return scro
    }()
    lazy var photoVm : AuthenCompanyMercPhotoVM = {
        let vm = AuthenCompanyMercPhotoVM.init(frame: CGRect.init(x: 0, y: 0, width: 0, height: 0))
        vm.licenceOcrBlock = {(dict)in
            self.msgVm.updateLicenceMsg(dict: dict)
        }
        return vm
    }()
    lazy var msgVm : AuthenCompanyMercMsgVM = {
        let vm = AuthenCompanyMercMsgVM.init(frame: CGRect.init(x: 0, y: self.photoVm.frame.maxY+kFitWidth(10), width: 0, height: 0))
        
        return vm
    }()
    lazy var nextBtn : UIButton = {
        let btn = UIButton()
        btn.frame = CGRect.init(x: kFitWidth(31), y: self.msgVm.frame.maxY+kFitWidth(40), width: kFitWidth(316), height: kFitWidth(48))
        btn.setTitle("保存并下一步", for: .normal)
        btn.setTitleColor(.white, for: .normal)
        btn.setBackgroundImage(UIImage.init(named: "button_theme_bg"), for: .normal)
        btn.layer.cornerRadius = kFitWidth(24)
        btn.clipsToBounds = true
        btn.titleLabel?.font = .systemFont(ofSize: 16, weight: .regular)
        btn.backgroundColor = .clear
        btn.isHidden = true
        btn.addTarget(self, action: #selector(nextAction), for: .touchUpInside)
        
        return btn
    }()
}

extension AuthenCompanyMercVM{
    @objc func nextAction() {
        if self.nextBlock != nil{
            self.nextBlock!()
        }
    }
    func checkValue() -> Bool {
        msgVm.getValue()
        if AuthenCompanyMsgModel.shareInstance.photoImgUrlLicence.count < 2{
            MCToast.mc_text("请上传营业执照")
            return false
        }
        if AuthenCompanyMsgModel.shareInstance.mchName.count < 1{
            MCToast.mc_text("请输入商户全称")
            return false
        }
        if AuthenCompanyMsgModel.shareInstance.mchNameSimple.count < 1{
            MCToast.mc_text("请输入商户简称")
            return false
        }
        if msgVm.merchantType == "2"{
            
        }else{
            if AuthenCompanyMsgModel.shareInstance.money.floatValue < 1{
                MCToast.mc_text("请输入注册资金")
                return false
            }
        }
        if AuthenCompanyMsgModel.shareInstance.licenseStartTime.count < 4{
            MCToast.mc_text("请选择经营期起始时间")
            return false
        }
        if AuthenCompanyMsgModel.shareInstance.licenseEndTime.count < 4{
            MCToast.mc_text("请选择经营期结束时间")
            return false
        }
        if AuthenCompanyMsgModel.shareInstance.mercMccName.count < 1{
            MCToast.mc_text("请选择行业分类")
            return false
        }
//        if AuthenCompanyMsgModel.shareInstance.mercAlipayMcc.count < 2{
//            MCToast.mc_text("请选择支付宝行业")
//            return false
//        }
//        if AuthenCompanyMsgModel.shareInstance.mercWeChatMcc.count < 2{
//            MCToast.mc_text("请选择微信行业")
//            return false
//        }
        if AuthenCompanyMsgModel.shareInstance.placeCode.count < 4{
            MCToast.mc_text("请选择地区码")
            return false
        }
        if AuthenCompanyMsgModel.shareInstance.licenseAddress.count < 10 || AuthenCompanyMsgModel.shareInstance.licenseAddress.count > 80{
            MCToast.mc_text("营业执照详细地址长度只能在10~80个字之间")
            return false
        }
//        if !WHBaseViewVC().judgeEmail(email: AuthenCompanyMsgModel.shareInstance.email){
//            MCToast.mc_text("请输入正确的电子邮箱")
//            return false
//        }
        if AuthenCompanyMsgModel.shareInstance.licenseArea.count < 4{
            MCToast.mc_text("请输入经营范围")
            return false
        }
        return true
    }
}
extension AuthenCompanyMercVM{
    func initUI(){
        addSubview(scrollView)
        scrollView.addSubview(photoVm)
        scrollView.addSubview(msgVm)
        scrollView.addSubview(nextBtn)
        
        scrollView.contentSize = CGSize.init(width: 0, height: nextBtn.frame.maxY+kFitWidth(20))
    }
    func updateUIForRefuse() {
        self.photoVm.updateUIForRefuse()
        self.msgVm.updateUIForRefuse()
    }
    func setReadMode() {
        nextBtn.isHidden = true
//        photoVm.isUserInteractionEnabled = false
        msgVm.isUserInteractionEnabled = false
        photoVm.setReadMode()
    }
}
